package com.cardbaobao.cardbabyclient.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

/**
 * 提示版本更新Dialog
 * Created by zad
 * 2017/1/4 19:14
 */

public class DownLoadDialog extends Dialog implements View.OnClickListener {

    private Context context;

    private TextView mTvUpdateInfo;

    public DownLoadDialog(Context context,String updateInfo) {
        super(context, R.style.dialog);
        setContentView(R.layout.dialog_down_load);
        this.context = context;
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initViews(updateInfo);
        initWindow();
    }

    private void initViews(String updateInfo) {
        mTvUpdateInfo = (TextView) findViewById(R.id.id_tv_update_info);
        mTvUpdateInfo.setText(updateInfo);
        findViewById(R.id.id_tv_left).setOnClickListener(this);
        findViewById(R.id.id_tv_right).setOnClickListener(this);
    }

    private void initWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtil.getScreenWidth(context) * 4 / 5;//设置对话框宽度
        params.gravity = Gravity.CENTER;//设置window在底部显示
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_tv_left://取消
                if(onDownLoadClickListener != null){
                    onDownLoadClickListener.onCancel();
                }
                break;
            case R.id.id_tv_right://确定
                dismiss();
                if(onDownLoadClickListener != null){
                    onDownLoadClickListener.onSubmit();
                }
                break;
        }
    }

    private OnDownLoadClickListener onDownLoadClickListener;

    public void setOnDownLoadClickListener(OnDownLoadClickListener onDownLoadClickListener){
        this.onDownLoadClickListener = onDownLoadClickListener;
    }

    public interface OnDownLoadClickListener{
        /**
         *  取消更新
         */
        void onCancel();
        /**
         *  确认更新
         */
        void onSubmit();
    }
}
